﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ctrip.Demo.Utils
{
    public static class ObjectUtil
    {

        /// <summary>
        /// 匿名类转换为字典
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static IDictionary<string, object> CreateValueDictionary(this object values)
        {
            if (values == null)
            {
                return null;
            }
            IDictionary<string, object> dictionary = values as IDictionary<string, object>;
            if (dictionary != null)
            {
                return dictionary;
            }
            dictionary = new Dictionary<string, object>();
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
            {
                object obj2 = descriptor.GetValue(values);
                dictionary[descriptor.Name] = obj2;
            }
            return dictionary;
        }

        /// <summary>
        /// 匿名类转换为字典
        /// </summary>
        /// <param name="values"></param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <returns></returns>
        public static IDictionary<string, object> CreateValueDictionary(this object values, bool ignoreCase)
        {
            if (!ignoreCase)
            {
                return values.CreateValueDictionary();
            }
            if (values == null)
            {
                return null;
            }
            Dictionary<string, object> dictionary = values as Dictionary<string, object>;
            if (dictionary != null)
            {
                if (dictionary.Comparer == StringComparer.OrdinalIgnoreCase)
                {
                    return dictionary;
                }
                return new Dictionary<string, object>(dictionary, StringComparer.OrdinalIgnoreCase);
            }
            dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
            {
                object obj2 = descriptor.GetValue(values);
                dictionary[descriptor.Name] = obj2;
            }
            return dictionary;
        }

        /// <summary>
        /// 匿名类转换为字典
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static IDictionary<string, string> CreateStringDictionary(this object values)
        {
            if (values == null)
            {
                return null;
            }
            IDictionary<string, string> dictionary = values as IDictionary<string, string>;
            if (dictionary != null)
            {
                return dictionary;
            }
            dictionary = new Dictionary<string, string>();
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
            {
                object obj2 = descriptor.GetValue(values);
                dictionary[descriptor.Name] = Convert.ToString(obj2);
            }
            return dictionary;
        }

        /// <summary>
        /// 匿名类转换为字典
        /// </summary>
        /// <param name="values"></param>
        /// <param name="ignoreCase">是否忽略大小写</param>
        /// <returns></returns>
        public static IDictionary<string, string> CreateStringDictionary(this object values, bool ignoreCase)
        {
            if (!ignoreCase)
            {
                return values.CreateStringDictionary();
            }
            if (values == null)
            {
                return null;
            }
            Dictionary<string, string> dictionary = values as Dictionary<string, string>;
            if (dictionary != null)
            {
                if (dictionary.Comparer == StringComparer.OrdinalIgnoreCase)
                {
                    return dictionary;
                }
                return new Dictionary<string, string>(dictionary, StringComparer.OrdinalIgnoreCase);
            }
            dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
            {
                object obj2 = descriptor.GetValue(values);
                dictionary[descriptor.Name] = Convert.ToString(obj2);
            }
            return dictionary;
        }
    }
}
